﻿using System;
using System.Collections.Generic;
using System.Text;

namespace WorkNodeFramework
{
    public class DateSetting
    {
        public DateSetting(DateTime? from, DateTime? to)
            : this(from, to, DateOrder.NONE)
        { }

        public DateSetting(DateTime? from, DateTime? to, DateType dateType)
            : this(from, to, dateType, DateOrder.NONE)
        {
        }

        public DateSetting(DateTime? from, DateTime? to, DateOrder order)
            :this(from,to,DateType.OpenDate,order)
        { 
        }

        public DateSetting(DateTime? from, DateTime? to, DateType dateType,DateOrder order)
        {
            From = from;
            To = to;
            this.order = order;
            DateType = dateType;
        }

        public DateType DateType { get; set; }

        public DateTime? From { get; set; }
        public DateTime? To { get; set; }
        DateOrder order = DateOrder.NONE;
        public DateOrder Order {
            get {
                return order;
            }
            set {
                order = value;
            }
        }

        internal void UpdateSelectQuery(Select select, Column dateColumn)
        {
            if (From != null)
                select.Where(WHERE.DO(dateColumn).IsEqualsOrGreaterThan(From.Value));
            if (To != null)
                select.Where(WHERE.DO(dateColumn).IsEqualsOrLeasterThan(To.Value));
            if (Order == DateOrder.ASC)
                select.Order(dateColumn, OrderByEnum.ASC);
            else if (Order == DateOrder.DESC)
                select.Order(dateColumn, OrderByEnum.DESC);
        }

        public static DateSetting OpenDate(DateTime? from, DateTime? to)
        {
            return new DateSetting(from, to,DateType.OpenDate);
        }

        public static DateSetting OpenDate(DateTime? from, DateTime? to, DateOrder order)
        {
            return new DateSetting(from, to, DateType.OpenDate,order);
        }

        public static DateSetting LastDate(DateTime? from, DateTime? to)
        {
            return new DateSetting(from, to, DateType.LastDate);
        }

        public static DateSetting LastDate(DateTime? from, DateTime? to, DateOrder order)
        {
            return new DateSetting(from, to, DateType.LastDate, order);
        }
    }

    public enum DateType { 
        OpenDate,
        LastDate
    }

    public enum DateOrder { 
        NONE,
        ASC,
        DESC
    }
}
